package no.uib.hplims.views.settings;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import no.uib.hplims.tools.ImportLibraryData;
import no.uib.hplims.tools.ImportOptimizedValues;

import com.vaadin.ui.Label;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.FailedEvent;
import com.vaadin.ui.Upload.FailedListener;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

public class ImportLibraryDataView extends VerticalLayout implements
		Upload.Receiver, Upload.FailedListener, Upload.SucceededListener {

	private static final long serialVersionUID = 463026806030091149L;
	
	private String HEADER_STYLE = Runo.LABEL_H2;

	private VerticalLayout libraryDataLayout;
	private Upload libraryDataUpload = new Upload();
	private VerticalLayout optimizedValuesLayout;
	private Upload optimizedValuesUpload = new Upload();
	private File file = null;

	public ImportLibraryDataView() {
		super();
		setCaption("Import data");
		setMargin(true, true, false, true);
		setSpacing(true);

		initLibraryDataUpload();
		initImportOptimizedValues();
	}
	
	/*
	 * Import optimized values from file
	 */
	
	private void initImportOptimizedValues() {
		optimizedValuesLayout = new VerticalLayout();
		optimizedValuesLayout.setSpacing(true);
		
		Label l = new Label("Import optimized values");
		l.setStyleName(HEADER_STYLE);
		optimizedValuesLayout.addComponent(l);
		optimizedValuesLayout.addComponent(new Label("Implementation not complete yet."));
		
		optimizedValuesUpload.addListener(optimizedValuesUploadSucceeded);
		optimizedValuesUpload.addListener(optimizedValuesUploadFailed);
		optimizedValuesUpload.setReceiver(optimizedValuesUploadReceiver);
		optimizedValuesLayout.addComponent(optimizedValuesUpload);
		
		addComponent(optimizedValuesLayout);
	}
	
	private SucceededListener optimizedValuesUploadSucceeded = new SucceededListener() {
		private static final long serialVersionUID = 641009833224903528L;

		public void uploadSucceeded(SucceededEvent event) {
			// Log the upload on screen.
			optimizedValuesLayout.addComponent(
					new Label("File " + event.getFilename() + " of type '"
							+ event.getMIMEType() + "' uploaded."));

			ImportOptimizedValues importer = new ImportOptimizedValues();
			boolean success = importer.startImport(file);
			
			if (success) {
				optimizedValuesLayout.addComponent(new Label("Import OK"));
			} else {
				optimizedValuesLayout.addComponent(new Label("Import failed"));
			}
			
			file = null;
		}
	};
	
	private FailedListener optimizedValuesUploadFailed = new FailedListener() {
		private static final long serialVersionUID = -1149862585749603660L;

		public void uploadFailed(FailedEvent event) {
			optimizedValuesLayout.addComponent(new Label("Uploading " + event.getFilename()
					+ " of type '" + event.getMIMEType() + "' failed."));
		}
	};

	private Receiver optimizedValuesUploadReceiver = new Receiver() {
		private static final long serialVersionUID = -7830941729257512381L;

		public OutputStream receiveUpload(String filename, String mimeType) {
			FileOutputStream fos = null;
			file = new File(filename);
			try {
				fos = new FileOutputStream(file);
			} catch (final java.io.FileNotFoundException e) {
				// Error while opening the file. Not reported here.
				e.printStackTrace();
				return null;
			}
			return fos;
		}
	};
	
	
	/*
	 * Import existing data from Google Docs library of heavy peptides.
	 */
	private void initLibraryDataUpload() {
		libraryDataUpload.addListener((SucceededListener) this);
		libraryDataUpload.addListener((FailedListener) this);
		libraryDataUpload.setReceiver((Receiver) this);
		
		Label l = new Label("Import heavy peptide library data");
		l.setStyleName(HEADER_STYLE);
		
		libraryDataLayout = new VerticalLayout();
		libraryDataLayout.setSpacing(true);
		libraryDataLayout.addComponent(l);
		libraryDataLayout.addComponent(libraryDataUpload);
		addComponent(libraryDataLayout);
	}

	public void uploadSucceeded(SucceededEvent event) {
		// Log the upload on screen.
		libraryDataLayout.addComponent(
				new Label("File " + event.getFilename() + " of type '"
						+ event.getMIMEType() + "' uploaded."));

		ImportLibraryData importer = new ImportLibraryData();
		boolean success = importer.startImport(file);
		if (success) {
			libraryDataLayout.addComponent(new Label("Import OK"));
		} else {
			libraryDataLayout.addComponent(new Label("Import failed"));
		}
		file = null;
	}

	public void uploadFailed(FailedEvent event) {
		libraryDataLayout.addComponent(new Label("Uploading " + event.getFilename()
				+ " of type '" + event.getMIMEType() + "' failed."));

	}

	public OutputStream receiveUpload(String filename, String mimeType) {
		FileOutputStream fos = null;
		file = new File(filename);
		try {
			fos = new FileOutputStream(file);
		} catch (final java.io.FileNotFoundException e) {
			// Error while opening the file. Not reported here.
			e.printStackTrace();
			return null;
		}
		return fos;
	}
}
